package com.example.papercutting.ui;


import android.content.Intent;
import android.graphics.Color;
import android.graphics.Path;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import  com.example.papercutting.ui.FoldedShapeView;

//import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import com.example.papercutting.R;

public class PartActivity extends AppCompatActivity {

    private  FoldedShapeView foldedShape;
    private String selectedPaperType; // 记录纸张类型
    private int selectedColor; // 记录选中的颜色
    private int foldCount = 1; // 默认折叠次数

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_part);

        // 接收从上一个页面传递的参数
        Intent intent = getIntent();
        selectedPaperType = intent.getStringExtra("selectedPaperType");
        selectedColor = intent.getIntExtra("selectedColor", Color.WHITE);

        // 初始化视图
        foldedShape = findViewById(R.id.folded_shape);
        ImageButton leftArrow = findViewById(R.id.left_arrow);
        ImageButton rightArrow = findViewById(R.id.right_arrow);
        Button fold4 = findViewById(R.id.fold_4);
        Button fold6 = findViewById(R.id.fold_6);
        Button fold8 = findViewById(R.id.fold_8);
        Button fold12 = findViewById(R.id.fold_12);

        // 初始化形状和颜色
        updateFoldedShape();

        // 左箭头返回上一页
        leftArrow.setOnClickListener(v -> {
            Intent backIntent = new Intent(PartActivity.this, ColorActivity.class);
            backIntent.putExtra("selectedPaperType", selectedPaperType);
            startActivity(backIntent);
        });

        // 右箭头跳转到下一步
        rightArrow.setOnClickListener(v -> {
            Toast.makeText(this, "当前选择", Toast.LENGTH_SHORT).show();
            Intent nextIntent = new Intent(PartActivity.this, CanvasActivity.class);
            nextIntent.putExtra("selectedPaperType", selectedPaperType);
            nextIntent.putExtra("color", selectedColor);
            nextIntent.putExtra("division", foldCount);
            startActivity(nextIntent);
        });

        // 折叠层数按钮点击事件
        fold4.setOnClickListener(v -> {
            foldCount = 4;
            updateFoldedShape();
        });

        fold6.setOnClickListener(v -> {
            foldCount = 6;
            updateFoldedShape();
        });

        fold8.setOnClickListener(v -> {
            foldCount = 8;
            updateFoldedShape();
        });

        fold12.setOnClickListener(v -> {
            foldCount = 12;
            updateFoldedShape();
        });
    }

    // 更新折叠后的形状
    private void updateFoldedShape() {
        int shapeType = getShapeTypeFromName(selectedPaperType);
        foldedShape.setShapeType(shapeType);
        foldedShape.setColor(selectedColor);
        foldedShape.setFoldCount(foldCount);
    }

    private int getShapeTypeFromName(String name) {
        switch (name.toLowerCase()) {
            case "circle":
                return FoldedShapeView.SHAPE_CIRCLE;
            case "octagon":
                return FoldedShapeView.SHAPE_OCTAGON;
            case "square":
                return FoldedShapeView.SHAPE_SQUARE;
            default:
                return FoldedShapeView.SHAPE_CIRCLE;
        }
    }
}
